//
//  Supermarket.swift
//  LoveFreshBeen
//
//  Created by Gao on 17/2/10.
//  Copyright © 2017年 Odm. All rights reserved.
//

import UIKit

class Categorie: NSObject {
    var id: String?
    var name: String?
    var sort: String?
}


class Products: NSObject {
    var a82: [Goods]?
    var a96: [Goods]?
    var a99: [Goods]?
    var a106: [Goods]?
    var a134: [Goods]?
    var a135: [Goods]?
    var a136: [Goods]?
    var a137: [Goods]?
    var a141: [Goods]?
    var a143: [Goods]?
    var a147: [Goods]?
    var a151: [Goods]?
    var a152: [Goods]?
    var a158: [Goods]?
    
    // 返回容器类中的所需要存放的数据类型 (以 Class 或 Class Name 的形式)。
    class func modelContainerPropertyGenericClass() -> [AnyHashable: Any] {
        return [
            "a82": Goods.self,
            "a96": Goods.self,
            "a99": Goods.self,
            "a106": Goods.self,
            "a134": Goods.self,
            "a135": Goods.self,
            "a136": Goods.self,
            "a137": Goods.self,
            "a141": Goods.self,
            "a143": Goods.self,
            "a147": Goods.self,
            "a151": Goods.self,
            "a152": Goods.self,
            "a158": Goods.self
        ]
    }

}

class SupermarketResouce: NSObject {
    var categories: [Categorie]?
    var products: Products?
    var trackid: String?
    
    // 返回容器类中的所需要存放的数据类型 (以 Class 或 Class Name 的形式)。
    class func modelContainerPropertyGenericClass() -> [AnyHashable: Any] {
        return [
            "categories": Categorie.self,
            "products": Products.self
        ]
    }

}

class Supermarket: NSObject {

    var code: Int = -1
    var msg: String?
    var reqid: String?
    var data: SupermarketResouce?
    
    // 返回容器类中的所需要存放的数据类型 (以 Class 或 Class Name 的形式)。
    class func modelContainerPropertyGenericClass() -> [AnyHashable: Any] {
        return ["data": SupermarketResouce.self]
    }
    
    class func loadSupermarketData(_ completion:(_ data: Supermarket?, _ error: NSError?) -> Void) {
        guard let path = Bundle.main.path(forResource: "supermarket", ofType: nil) else{
            return
        }
        
        let url = URL(fileURLWithPath: path)
        
        guard let jsonString = try? String(contentsOf: url) else{
            return
        }
        
        let supermarket = Supermarket.yy_model(withJSON: jsonString)
        
        if supermarket != nil {
            completion(supermarket, nil)
        }
    }
    
    class func searchCategoryMatchProducts(_ supermarketResouce: SupermarketResouce) -> [[Goods]]? {
        var arr = [[Goods]]()
        
        let products = supermarketResouce.products
        for cate in supermarketResouce.categories! {
            let goodsArr = products!.value(forKey: cate.id!) as! [Goods]
            arr.append(goodsArr)
        }
        return arr
    }
    
}
